<?php

namespace App\Http\Controllers\admin;

use DB;
use Hash;
use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;
//模型
use App\Models\Staff;
use App\Models\Areastaff;
use App\Models\Roommember;
class StaffController extends PublicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        /*搜索条件始*/
        //搜索条件
        $get['name'] = $request -> input('name','');
        if($get['name']){
            $this -> where .= ' -> where("name","like","%".$get["name"]."%")';
        }
        $get['email'] = $request -> input('email','');
        if($get['email']){
            $this -> where .= ' -> where("email","like","%".$get["email"]."%")';
        }
        $get['phone'] = $request -> input('phone','');
        if($get['phone']){
            $this -> where .= ' -> where("phone","like","%".$get["phone"]."%")';
        }
        /*搜索条件末*/
        /*代理商搜索始*/
        //获取自身及旗下所有代理商
        $agentInfo = $this -> agentInfo($request -> cookie('logon')['agent_id']);
        /*代理商搜索末*/
        $info = '';
        eval('$info = DB::table("staff") 
        -> where("agent_id",$this -> agent_id) 
        -> select("id","name","email","phone","status")'
        .$this-> where.
        '-> paginate(10);');
        $data = $info -> all();
        if($data){
            foreach ($data as $key=>$val){
                $data[$key]['range']['area'] = $this -> range('area',$val['id']);
            }
        }
        $get['agent_id'] = $this -> agent_id;
        return view('admin.staff.index',['data'=>$data,'agentInfo'=>$agentInfo,'info'=>$info,'request'=>$request -> all(),'get'=>$get,'areaids' => $this->area_ids]);
    }

    //获取管理范围
    public function range($range,$member_id){
            //获取管理区域
            $rangeArr = DB::table($range.'_staff as s')
                -> leftJoin($range.' as r','r.id','=','s.'.$range.'_id')
                -> select('r.name')
                -> where('s.staff_id',$member_id)
                -> get();
            $rangeStr = '';
            if($rangeArr){
                foreach ($rangeArr as $v){
                    $rangeStr .= $v['name'].',';
                }
            }
            return substr($rangeStr,0,-1);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        //获取员工角色
        $role = DB::table('staff_role') -> get();
        return view('admin.staff.create',['role'=>$role]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['agent_id'] = $this -> agent_id;
        $info['pwd'] = Hash::make($info['pwd']);
        //添加区域表
        $res = DB::table('staff') -> insert($info);
        //$res = Staff::create($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        return $data;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {//获取员工角色
        $role = DB::table('staff_role') -> get();
        $id = $request -> input('id');
        $data = Staff::find($id) -> toarray();
        return view('admin.staff.edit',['data'=>$data,'role'=>$role]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        $res = DB::table('staff') -> where('id',$id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询区域下是否有建筑,有不能删除
        $count = Areastaff::where('staff_id',$id) -> count();
        if($count == 0){
            //查询区域和客户的关系表并删除
            $res = Staff::destroy($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }
}
